using System.Collections.Generic;
using System.Windows.Browser;

namespace HtmlUtilities.UITest
{
    [ScriptableType]
    public class ScriptObjectCommands
    {
        public ScriptObjectCommands()
        {
            Html.Page.RegisterScriptableObject("SOTest", this);
        }

        public IEnumerable<Command<string>> GetCommands()
        {
            return new List<Command<string>>
                       {
                           new Command<string>("CheckAccess", () => TestDiv.CheckAccess().ToString()),
                           new Command<string>("ConvertTo", ConvertTo),
                           new Command<string>("Dispatcher", () => "I am not sure how to test this"),
                           new Command<string>("GetProperty_Indexed", GetPropertyIndexed),
                           new Command<string>("GetProperty_Named", GetPropertyNamed),
                           new Command<string>("Invoke", () => "See ConvertTo or GetProperty"),
                           new Command<string>("InvokeSelf", InvokeSelf),
                           new Command<string>("ManagedObject", ManagedObject),
                           new Command<string>("SetProperty_Indexed", SetPropertyIndexed),
                           new Command<string>("SetProperty_Named", SetPropertyNamed)
                       };
        }

        private static IHtmlElement TestDiv { get { return Html.Page.Document.GetElementById("testing"); } }

        private static string SetPropertyNamed()
        {
            var result = Html.Page.Window.Invoke("CreateName", "Brian") as IScriptObject;
            result.SetProperty("Name", "Cara");
            return result.GetProperty("Name").ToString();
        }

        private static string SetPropertyIndexed()
        {
            var result = Html.Page.Window.Invoke("GetList", 5) as IScriptObject;
            result.SetProperty(2, "Modified Value");
            var resultArray = result.ConvertTo<string[]>();
            return string.Join(", ", resultArray);
        }

        private string ManagedObject()
        {
            var result = Html.Page.Window.Invoke("GetScriptableObject") as IScriptObject;
            return "ManagedObject is: " + result.ManagedObject.GetType();
        }

        private static string InvokeSelf()
        {
            var listFunc = Html.Page.Window.GetProperty("GetList") as IScriptObject;
            var stringList = listFunc.InvokeSelf(5) as IScriptObject;
            return stringList.GetProperty(2).ToString();
        }

        private static string GetPropertyNamed()
        {
            var result = Html.Page.Window.Invoke("CreateName", "Brian") as IScriptObject;
            return result.GetProperty("Name").ToString();
        }

        private static string GetPropertyIndexed()
        {
            var result = Html.Page.Window.Invoke("GetList", 5) as IScriptObject;
            return result.GetProperty(2).ToString();
        }

        private static string ConvertTo()
        {
            var result = Html.Page.Window.Invoke("GetList", 5) as IScriptObject;
            var resultArray = result.ConvertTo<string[]>();
            return string.Join(", ", resultArray);
        }
    }
}